package com.runvp.common.util.pdf;

import com.github.pagehelper.util.StringUtil;
import com.runvp.common.exception.base.BaseException;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

@Slf4j
public class HtmlUtil {

    private String filePath;
    private String content;

    public static HtmlUtil instance(){
        return new HtmlUtil();
    }

    public HtmlUtil setFilePath(String filePath) {
        this.filePath = filePath;
        return this;
    }

    public HtmlUtil setContent(String content) {
        this.content = content;
        return this;
    }

    private HtmlUtil(){

    }

    /**
     * 创建html
     * @return str
     */
    public String createHtml() {
        if(StringUtil.isEmpty(filePath)){
            throw new BaseException("报告异常：报告生成路径为空");
        }
        File file = new File(filePath);
        try {
            file.createNewFile();//创建文件
            PrintStream printStream = new PrintStream(new FileOutputStream(file));
            printStream.println(content);//将字符串写入文件
        } catch (IOException e) {
            e.printStackTrace();
        }
        return filePath;
    }
    /**
     * 删除html
     */
    public  void deleteHtml(){
        if(StringUtil.isEmpty(filePath)){
            return;
        }
        File file = new File(filePath);
        boolean flag = file.delete();
        log.info("deleteHtml ===>flag:{} ",flag);
    }

    public static void main(String[] args) throws IOException {
//        String path = "D:/dev/nginx/html/3.1/report/";
//        Date todaytime = new Date();
//        SimpleDateFormat ttime = new SimpleDateFormat("yyyyMMddhhMMSS");
//        String name = ttime.format(todaytime);//生成当前时间作为文件名
//        String content = "我是好人";
//        content = "<!DOCTYPE html><html><head><meta charset=\"utf-8\"><meta content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0\" name=\"viewport\"><meta name=\"apple-mobile-web-app-status-bar-style\" content=\"black\" /><meta name=\"format-detection\" content=\"telephone=yes\" /><meta name=\"apple-mobile-web-app-capable\" content=\"yes\" /><title>评级批次报告</title><link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" /></head><body class=\"mainContent\"><div class=\"title\"><h2>关于企业评级模型5<br />评级批次报告</h2><div class=\"text-right\"><a  download=\"下载报告名称.pdf\" href=\"#\" class=\"label large blue\">PDF报告下载</a></div></div><ul class=\"listBox\"><li><label for=\"\">评级时间：</label><span>2022-01-12 11:32:43</span></li><li><label for=\"\">评级批次号：</label><span>XYPJ-1641958363246</span></li><li><label for=\"\">匹配主体数：</label><span>0</span></li><li><label for=\"\">评分未命中区间主体数：</label><span>0</span></li></ul><h4>评价结果分布情况</h4><div class=\"mytable\"><table><thead><tr><th>评价等级</th><th>AAAA</th><th>AAA</th><th>AA</th><th>A</th><th>B</th></tr></thead><tbody><tr><td>命中主体数</td><td>0</td><td>0</td><td>0</td><td>1</td><td>0</td></tr><tr><td>占比</td><td>0.00%</td><td>0.00%</td><td>0.00%</td><td>100.00%</td><td>0.00%</td></tr></tbody></table></div><h4>评价结果明细</h4><div class=\"mytable\"><table class=\"w-100\"><thead><tr><th>企业名称</th><th>统一社会信用代码</th><th>得分</th><th width=\"30\">等级</th></tr></thead><tbody class=\"resDetail\"><tr><td>测试企业</td><td>11111</td><td>100</td><td>A</td></tr></tbody></table></div><div class=\"loading\"><img src=\"loading.gif\" >加载中...</div><script src=\"jquery.min.js\" type=\"text/javascript\" charset=\"utf-8\"></script><script> var page = 1var size = 10var baseUrl = ''var loading = falsevar loadEle = $('.loading')$(document).ready(function() {//初始化完毕 执行事件scrollEvent(getTable) }) // 获取表格数据 function getTable(){ loadEle.show() page++ $.ajax({url: baseUrl+'',success: function(res) {//成功后回调dataHtml(res.data)loadEle.hide()},error: function(err) {//失败后回调} }) } //拼接数据function dataHtml (data) {var tr = ''data.forEach(function(item,index){tr += '<tr>'+'<td>上海新世界股份有限公司</td>'+'<td>**********</td>'+'<td>9999</td>'+'<td>A</td>'+'</tr>'})$('.resDetail').append(tr)}// 封装滚动函数function scrollEvent (fn) {$(window).scroll(function() {if ($(document).height() - $(window).height() - $(document).scrollTop() <= 0 && !loading) {loading = trueif(fn){fn()}  console.log(\"滚动条已经到达底部为\" + $(document).scrollTop());}else{loading = false}});}</script></body></html>";
////        HtmlUtil.instance().setFilePath(path+name+".html").setContent(content).createHtml();
        PDFUtil.createPdf("D:/dev/nginx/html/3.1/authFile/company.html","D:/dev/nginx/html/3.1/authFile/company.pdf","D:/dev/nginx/html/3.1/authFile/simsun.ttf");
//        HtmlUtil.instance().setFilePath("D:\\dev\\nginx\\html\\3.1\\report\\58f67224dfaf4bf3bc5b8d4a1c83c974.html").deleteHtml();
//        PDFUtil.createPdf1("D:/dev/nginx/html/3.1/report/","D:/dev/nginx/html/3.1/report/884726aeef6343959963833acbb9f855.html","D:/dev/nginx/html/3.1/report/884726aeef6343959963833acbb9f855.pdf","D:/dev/nginx/html/3.1/report/simsun.ttf");

    }

}